// (c)2016 Flipboard Inc, All Rights Reserved.

package com.viroyal.monitorlib.network;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import com.hwangjr.rxbus.RxBus;
import com.suntiago.baseui.utils.log.Slog;
import com.viroyal.monitorlib.BuildConfig;
import com.viroyal.monitorlib.Config;
import com.viroyal.monitorlib.entity.rx.RxHostChanged;
import com.viroyal.monitorlib.utils.MacUtil;
import com.viroyal.monitorlib.utils.SPUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import retrofit2.Retrofit;

public class Api {
  private static final String TAG = Api.class.getSimpleName();
  private static Api sApi;
  private HashMap<Class, Object> mApiObjects;
  private HashMap<Class, Object> mApiObjects2;


  public static Config mConfig = Config.test();//Config.zhushan();
  public static String DEFAULT_HOST = "";

  //deepstreem相关 api
  public static String BASE_URL = "http://" + DEFAULT_HOST + ":8000/";
  private static String UPDATE_HOST = DEFAULT_HOST + ":6020";
  //private static String GETAPI_URL = "http://viotapi.iyuyun.net:9300/";
  private static String GETAPI_URL_MCPAPI = BuildConfig.API_HOST;
  //通过netty连接后台服务器 网络地址配置
  public static String NETTY_HOST = "";
  public static int NETTY_HOST_PORT = 0;
  public String deviceId = "";
  public static String schoolId = "";

  private Api() {
    mApiObjects = new HashMap<>();
    mApiObjects2 = new HashMap<>();
  }

  public static void init(Context context) {
    Api.get().UPDATE_HOST = SPUtils.getInstance(context).get("UPDATE_HOST", UPDATE_HOST);
    Api.get().BASE_URL = SPUtils.getInstance(context).get("MonitorIp", Api.get().BASE_URL);
  }

  public static Api get() {
    if (sApi == null) {
      sApi = new Api();
    }
    return sApi;
  }

  public synchronized <T> T getApi(Class<T> tClass) {
    if (mApiObjects != null) {
      if (mApiObjects.get(tClass) != null) {
        return (T) mApiObjects.get(tClass);
      }
    } else {
      mApiObjects = new HashMap<>();
    }
    Retrofit retrofit = new Retrofit.Builder()
        .client(HttpManager.getHttpClient(HttpLoggingInterceptor.Level.BODY))
        .baseUrl(BASE_URL)
        .addConverterFactory(HttpManager.sGsonConverterFactory)
        .addCallAdapterFactory(HttpManager.sRxJavaCallAdapterFactory)
        .build();
    mApiObjects.put(tClass, retrofit.create(tClass));
    return (T) mApiObjects.get(tClass);
  }

  public synchronized <T> T getApi2(Class<T> tClass) {
    if (mApiObjects2 != null) {
      if (mApiObjects2.get(tClass) != null) {
        return (T) mApiObjects2.get(tClass);
      }
    } else {
      mApiObjects2 = new HashMap<>();
    }
    Retrofit retrofit = new Retrofit.Builder()
        .client(HttpManager.getHttpClient(HttpLoggingInterceptor.Level.BODY))
        .baseUrl(GETAPI_URL_MCPAPI)
        .addConverterFactory(HttpManager.sGsonConverterFactory)
        .addCallAdapterFactory(HttpManager.sRxJavaCallAdapterFactory)
        .build();
    mApiObjects2.put(tClass, retrofit.create(tClass));
    return (T) mApiObjects2.get(tClass);
  }

  public void setApi(String host) {
    BASE_URL = host;
//        BASE_URL = "http://47.100.19.248:8091/";
    mApiObjects.clear();
  }

  public void setApiUPDATE(String host) {
    if (host != null && !host.equals(UPDATE_HOST)) {
      UPDATE_HOST = host;
      RxBus.get().post(new RxHostChanged());
    }
  }

  public String getApiUPDATE() {
    return UPDATE_HOST;
  }

  public String getApi() {
    return BASE_URL;
  }

  public String getSchoolId() {
    return schoolId;
  }

  public void setApiConfig(String api, String netty_host, int netty_post, String school_id) {
    String api_host = api.subSequence(7, api.length() - 6).toString();
    DEFAULT_HOST = api_host;
    BASE_URL = api;
    NETTY_HOST = netty_host;
    NETTY_HOST_PORT = netty_post;
    schoolId = school_id;
    this.mApiObjects.clear();
//        DEFAULT_HOST = "47.100.19.248";
//        BASE_URL = "http://47.100.19.248:8091";
//        NETTY_HOST = "47.100.19.248";
//        NETTY_HOST_PORT = 9101;
//        schoolId = "1004";
  }

  public String getDeviceId(Context context) {
    if (!TextUtils.isEmpty(deviceId)) {
      return deviceId;
    }
//    deviceId = DeviceInfoUtil.getInstance(context).getDeviceUuid();
    String path = Environment.getExternalStorageDirectory() + File.separator +
        "viroyal_mac.txt";
    File file = new File(path);
    FileInputStream in;
    try {
      in = new FileInputStream(file);
      int l = (int) file.length();
      byte[] filecontent = new byte[l];
      in.read(filecontent);
      in.close();
      deviceId = new String(filecontent);
      Slog.d(TAG, "getDeviceId  []: from viroyal_mac " + deviceId);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    if (TextUtils.isEmpty(deviceId)) {
      deviceId = MacUtil.getLocalMacAddressFromIp();
      Slog.d(TAG, "getDeviceId  []: from getLocalMacAddressFromIp " + deviceId);
    }
    return deviceId;
  }
}
